﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate;
using NHibernate.Criterion;
using Rodunu.Movies.Contracts;
using Rodunu.Movies.Data;
using Rodunu.Movies.Interfaces.Movie;
using Rodunu.Movies.Interfaces.User;
using Rodunu.Movies.Logic.User;

namespace Rodunu.Movies.Logic.Movie
{
    public class FriendMovieRequestLogic : Repository<FriendMovieRequest>, IFriendMovieRequest
    {
        #region IFriendMovieRequest Members

        public IList<FriendMovieRequest> GetPendingMovieRequestsForUser(long userId)
        {
            string sqlCommand = "SELECT * from FriendMovieRequest fmr left join UserMovies um on um.Id = fmr.UserMovieId where um.UserId = " + userId + " and Approved Is null";
            ISession session = DBHelper.GetCurrentSession();
            return session.CreateSQLQuery(sqlCommand, "FriendMovieRequest", typeof(FriendMovieRequest)).List<FriendMovieRequest>().OrderBy(o => o.UserInfo.Id).ToList();
        }

        public IList<FriendMovieRequest> GetUnreturnedMoviesForUser(long userId)
        {
            string sqlCommand = "SELECT *  FROM [RMovies].[dbo].[FriendMovieRequest] fmr left join usermovies um on um.id = fmr.usermovieid where um.userid = " + userId + " and approved = 1 and returndate is null";
            ISession session = DBHelper.GetCurrentSession();
            return session.CreateSQLQuery(sqlCommand, "FriendMovieRequest", typeof(FriendMovieRequest)).List<FriendMovieRequest>().OrderBy(o => o.UserInfo.Id).ToList();
        }

        public IList<FriendMovieRequest> GetUnreturnedMoviesForFriend(long userId)
        {
            ISession session = DBHelper.GetCurrentSession();
            IList<FriendMovieRequest> friendMovieRequest = session.CreateCriteria(typeof(Contracts.FriendMovieRequest))
                .Add(Expression.Eq("UserInfo.Id", userId))
                .Add(Expression.Eq("Approved", true))
                .Add(Expression.IsNull("ReturnDate"))
                .List<FriendMovieRequest>();
            //DBHelper.CloseSession();
            return friendMovieRequest;
        }

        public FriendMovieRequest CheckIfMovieAvailable(long movieId)
        {
            ISession session = DBHelper.GetCurrentSession();
            var friendMovieRequest = (FriendMovieRequest)session.CreateCriteria(typeof(Contracts.FriendMovieRequest))
                .Add(Expression.Eq("UserMovies.Id", movieId))
                .Add(Expression.Eq("Approved", true))
                .Add(Expression.IsNull("ReturnDate"))
                .UniqueResult();
            //DBHelper.CloseSession();
            return friendMovieRequest;
        }

        public FriendMovieRequest GetByMovieId(long movieId)
        {
            ISession session = DBHelper.GetCurrentSession();
            var friendMovieRequest = (FriendMovieRequest)session.CreateCriteria(typeof(Contracts.FriendMovieRequest))
                .Add(Expression.Eq("UserMovies.Id", movieId))
                .UniqueResult();
            //DBHelper.CloseSession();
            return friendMovieRequest;
        }

        #endregion
    }
}
